<template>
  <div class="main_spac1 maincl">
    <div class="main_white padding10 paddingBottom20">
      <div class="mainor_form">
        <div class="mainor_te">
          <!-- type="primary" -->
          <el-button @click="bulk_final_export">导出</el-button>
        </div>
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="70px"
        >
          <el-form-item label="单号" prop="related_order_sn">
            <el-input
              v-model.trim="form.related_order_sn"
              placeholder="结算单号/批发单号/账单号"
              clearable
              class="wid220"
            ></el-input>
          </el-form-item>
          <el-form-item label="结算日期" prop="createTime">
            <el-date-picker
              v-model="createTime"
              type="daterange"
              class="time260"
              :editable="false"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :clearable="true"
              :picker-options="pickerOptions"
            />
          </el-form-item>

          <el-form-item>
            <el-button class="reset marginLeft10" type="primary" @click="reset"
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <div class="main_table" style="min-height: 570px">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :header-cell-class-name="this.$verticaline"
          show-summary
          :summary-method="getSummaries"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" />

          <el-table-column label="批发商" prop="" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{ scope.row.dispatch ? scope.row.dispatch.company_name : "" }}
            </template>
          </el-table-column>
          <el-table-column
            label="结算单号"
            prop="order_sn"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="批发单号"
            prop="related_order_order_sn"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="账单号"
            prop="related_order_sn"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="下单金额"
            prop="order_money"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="账单金额"
            prop="account_money"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="差异金额"
            prop="diff_money"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="备注"
            prop="remark"
            :show-overflow-tooltip="true"
          />
        </el-table>
      </div>
      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { bulk_final, bulk_final_export } from "@/api/bulk/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import {
  recursion,
  category,
  parseTime,
  getyesterday,
  exportMethod,
} from "@/utils/index";
import dateOption from "@/options/dateOption";

export default {
  data() {
    return {
      exportMethod,
      createTime: [],
      pickerOptions: dateOption,
      // 搜索
      form: {
        related_order_sn: "", //单号
        account_order_start: "", //结算开始日期
        account_order_end: "", //结算结束日期
      },

      // 更多
      moredata: false,
      // 列表数据
      list: [],
      listLoading: false,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 合计数据
      total: {
        order_money: 0,
        diff_money: 0,
        account_money: 0,
      },

      selectArr: [],
    };
  },
  methods: {
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      console.log(val);
      val.map((item) => {
        this.selectArr.push(item.id);
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      //   this.order_index();
    },
    changePage(page) {
      this.current.page = page;
      //   this.order_index();
    },

    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.bulk_final();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.createTime = [];
      this.bulk_final();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    bulk_final() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      bulk_final(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.total.order_money = 0;
          this.total.diff_money = 0;
          this.total.account_money = 0;
          res.data.list.data.map((e) => {
            this.total.order_money += parseFloat(e.order_money);
            this.total.diff_money += parseFloat(e.diff_money);
            this.total.account_money += parseFloat(e.account_money);
          });
          this.list = res.data.list.data;
          this.totalCount = res.data.list.total;
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.account_order_start = this.createTime ? this.createTime[0] : "";
      form.account_order_end = this.createTime ? this.createTime[1] : "";
      return form;
    },

    // 合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        switch (index) {
          case 1:
            sums[index] = "合计";
            break;
          case 5:
            sums[index] = this.$toFixed(this.total.order_money);
            break;
          case 6:
            sums[index] = this.$toFixed(this.total.account_money);
            break;
          case 7:
            sums[index] = this.$toFixed(this.total.diff_money);
            break;
        }
      });

      return sums;
    },

    // 导出
    bulk_final_export() {
      console.log(this.selectArr);
      if (this.selectArr.length == 0) {
        this.$message.warning("请勾选需要导出的数据");
        return;
      } else {
        let ids = this.selectArr.join(",");
        let data = Object.assign(
          {
            sign: Md5(
              objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
            ),
          },
          apiUrl().name,
          { ids: ids }
        );
        bulk_final_export(data).then((res) => {
          this.exportMethod(res, "批发结算单");
        });
      }
    },
  },
  created() {
    // 列表
    this.bulk_final();
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.mainor_form form {
  width: calc(100% - 280px);
}
</style>
